<?php
class Conversation
{
	public $conversationparticipants = "";
	public $convCreator = "";
	public $convName = "";
	public $messageText;
	public $exitCode = "2";
	public $exitText = "";
	
	public function setGroup($convName,$user)
	{
		if($convName != null)
		{
			$this->convName = $convName;
			$this->convNameSet = true;
		}
		else
		{
			$this->convNameSet = false;
		}
		if($user->userName == "")
		{
			$this->userNameSet = false;
		}
		else
		{
			$this->userName = $user->userName;
			$this->userNameSet = true;
		}
		
		if($this->userNameSet && $this->convNameSet)
		{
			$this->getListGroups();
			$this->isParticipant();
			$this->isGroupOwner();
		}
		else
		{
			$this->giveExitStatus(1,"No username was provided");
		}
	}
	
	private function giveExitStatus($exitCode,$exitText)
	{
		$this->exitCode = $exitCode;
		$this->exitText = $exitText;
	}
	
	public function addMessage()
	{
		if($this->userName != "" && $this->convName != "" && $this->newMessage)
		{
			#stopped here.
			crowFather("insert into message (convParticipant,convName,convMessage)");
		}
		else
		{
			$this->setExitStatus(1,"The values for userName , convName or newMessage have not been set");
		}
	}
	
	public function checkConversationExists($name)
	{
		if($name != "")
		{
			
			$this->convName = $name;
			$this->groupIds = array();
			
			if(crowFather("select count(*) from groups where groupName = " . crowFather($this->convName,6) . ";",5) > 1)
			{
				foreach(crowFather("select groupId from groups where groupName = " . crowFather($this->convName,6) . ";",4) as $groupCol)
				{
					foreach($groupCol as $groupRow)
					{
						array_push($this->groupIds,$groupRow);
					}
				}
				$this->giveExitStatus(0,"Group exists");
				return true;
			}
			else
			{
				$this->giveExitStatus(1,"Group dosen't exist");
				return false;
			}
			
		}
	}
	
	public function getListGroups()
	{
		if($this->userNameSet)
		{
			$this->groupIds = array();
				
			$groupSQLArray = crowFather("select groupId from participant where userName = " . crowFather($this->userName,6) . ";",4);
				
			foreach($groupSQLArray as $groupCol)
			{
				foreach($groupCol as $groupRow)
				{
					array_push($this->groupIds,$groupRow);
				}
			}
			$this->giveExitStatus(0,"Group list created");
		}
		else
		{
			$this->giveExitStatus(1,"No user to search with was given");
		}
	}
	
	public function getGroupOptions($user_Search)
	{
		if($user_Search != "")
		{
			$this->getListGroups($user_Search);
			$this->giveExitStatus(0,"The groupId HTML was provided");
			return dataFieldConstructor($this->groupIds,1);
		}
		else
		{
			$this->giveExitStatus(1,"The user was not set");
		}
	}
	
	public function addParticipant($group,$authUser,$newUser)
	{
		if($this->isGroupOwner($authUser,$group))
		{
			if(crowFather("select count(*) from participant where userName = " . crowFather($newUser->userName ,6) .  " and groupId = " . crowFather($group,6) . ";",5) == 0)
			{
				crowFather("insert into participant (groupId,userName) values ( " . crowFather($group,6) . " , " .  crowFather($newUser->userName,6) . ");",3);
				$this->giveExitStatus(0,"The user has been added as a participant to the group");
			}
			else
			{
				$this->giveExitStatus(1,"The user is already a part of this group");
			}
		}
		else
		{
			$this->giveExitStatus(1,"The user provided is not the owner of this group");
		}
	}
	
	public function isParticipant()
	{
		if($this->userNameSet)
		{
			$this->isParticipantIn = array();
			$this->isActiveConversation = array();
			$this->isParticipant = false;
			$this->getListGroups($this->userName);
			
			if($this->groupIds != null);
			{
				foreach($this->groupIds as $group)
				{
					if(crowFather("select count(*) from participant where userName = " . crowFather($this->userNameSet,6) . " and groupId = " . crowFather($group,6) . ";",5) == 1)
					{
						array_push($this->isActiveConversation,crowFather("select activeGroup from group where groupId = " . crowFather($group,6) . ";",5));
						array_push($this->isParticipantIn,$group);
						$this->isParticipant = true;
						$this->giveExitStatus(0,"Found as participant");
					}
				}
				
				if($this->isParticipant == false)
				{
					$this->giveExitStatus(1,"User was not a participant in any of the groups");
				}
				
				return $this->isParticipant;
			}
		}
		else
		{
			$this->giveExitStatus(1,"The user name was not set");
		}
		
	}
	
	public function isGroupOwner()
	{
		if(crowFather("select count(*) from groups where groupId = '" . $this->convName . "' and groupOwner = " . crowFather($this->userName,6) . ";",5) > 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}	
	
	public function newConversation()
	{
		if(crowFather("select count(*) from groups where convName = " . crowFather($this->convName,6) . ";",5) == 0)
		{
			$this->sqlQuery = "insert into groups(groupName,groupOwner,activeGroup) values (" . crowFather($this->convName,6) . "," . crowFather($this->userName ,6) . ",'Y');";
			crowFather($this->sqlQuery,3);
			$this->giveExitStatus(0,"Conversation has been created now");
		}
		else
		{
			$this->giveExitStatus(1,"A group with the given name already exists");
		}
	}
	
	public function messageOutput($valueId,$messageIdArray)
	{
		if($valueId == 1)
		{
			$this->sqlArray = crowFather("select messageSender,messageRecipient,messageText from message where convId = '" . $messageIdArray . "';",4);
			$this->returnData = "<table>";
			foreach($this->sqlArray as $row)
			{
				$this->returnData .= "<tr>";
				foreach($row as $col)
				{
					$this->returnData .= "<td class='tableContentCell' >" . $col . "</td>";
				}
				$this->returnData .= "</tr>";
			}
			$this->returnData .= "</table>";
		}
	}
	
	public function deleteGroup()
	{
		if($this->selectedGroupID != "")
		{
			if(crowFather("select count(*) from group where groupId = " . crowFather($this->selectedGroupID,6) . ";",5) == 1)
			{
				$this->sqlArray = crowFather("update group where groupId = " . crowFather($this->selectedGroupID,6) . " set activeGroup = 'N';",3);
				$this->giveExitStatus(0,"The group has been deleted");
			}
			else
			{
				$this->giveExitStatus(1,"The groupId provided dosen't exist.");
			}
		}
		else
		{
			$this->giveExitStatus(1,"No groupID was assigned to selectedGroupID");
		}
	}
	
	
}
?>